import time
import unittest
from time import sleep
from selenium.common import NoSuchElementException
from base.base_page import BasePage


class ExportPage(BasePage, unittest.TestCase):
    # 横屏查看
    def landscape_btn(self):
        return self.by_id(self.HW_ENV + 'img_land').click()

    # 导出
    def export_btn(self):
        return self.by_id(self.HW_ENV + 'btn_export').click()

    def btn_4_complete(self):
        return self.by_id(self.HW_ENV + 'btn_4_complete').click()

    # Txt文件
    def txt_select(self):
        return self.by_uiautomator('new UiSelector().text("Txt文件")').click()

    def picture_select(self):
        return self.by_uiautomator('new UiSelector().text("图片")').click()

    def long_picture_select(self):
        return self.by_uiautomator('new UiSelector().text("长图")').click()

    def local_btn(self):
        return self.by_uiautomator('new UiSelector().text("本地")').click()

    def album_btn(self):
        return self.by_uiautomator('new UiSelector().text("相册")').click()

    def wechat_btn(self):
        return self.by_uiautomator('new UiSelector().text("微信")').click()

    # 微信-文件传输助手
    def wechat_computer(self):
        return self.by_uiautomator('new UiSelector().text("文件传输助手")').click()

    # 微信-文件传输助手-分享
    def wechat_share(self):
        return self.by_id('com.tencent.mm:id/guw').click()

    # 微信-返回第三方工具
    def wechat_back(self):
        return self.by_id('com.tencent.mm:id/gui').click()

    def more_btn(self):
        return self.by_uiautomator('new UiSelector().text("更多")').click()

    # 无痕模式
    def not_save(self):
        return self.by_id(self.HW_ENV + 'traceless_tv')

    # 发送到我的电脑
    def send_my_computer(self):
        return self.by_uiautomator('new UiSelector().text("发送到我的电脑")').click()

    # 完成
    def btn_complete(self):
        return self.by_id(self.HW_ENV + 'btn_complete').click()

    # 文件名
    def txt_title(self):
        return self.by_id(self.HW_ENV + 'txt_title').click()

    # 清空文件名
    def clear_txt_title(self):
        return self.by_id(self.HW_ENV + 'folder_cross').click()

    # 文件名输入框
    def newfolder_edit(self):
        return self.by_id(self.HW_ENV + 'newfolder_edit')

    # 重命名(导出长图）-确定
    def sure_btn(self):
        return self.by_id(self.HW_ENV + 'sure').click()

    # 返回
    def back_btn(self):
        return self.by_id(self.HW_ENV + 'navibar_back').click()

    def do_edit_filename(self, filename):
        """
        重命名文件
        :param filename: 文件名
        """
        tm = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
        filename = filename + ' ' + tm
        self.driver.implicitly_wait(2000)
        try:
            self.assertTrue(self.by_id(self.HW_ENV + 'txt_title'))
        except NoSuchElementException as e:
            self.take_screenshot(filename + '重命名失败')
            return e
        else:
            self.txt_title()
        sleep(1)
        self.clear_txt_title()
        sleep(1)
        self.newfolder_edit().send_keys(filename)
        sleep(1)
        self.sure_btn()
        sleep(1)

    # 导出功能
    def export_to_wechat(self):
        self.wechat_btn()
        try:
            self.driver.implicitly_wait(1)
            self.sure_btn()
        except NoSuchElementException:
            pass
        self.driver.implicitly_wait(2000)
        self.wechat_computer()
        self.driver.implicitly_wait(2)
        self.wechat_share()
        self.driver.implicitly_wait(2)
        self.wechat_back()
        sleep(2)

    def export_to_qq(self):
        try:
            self.driver.implicitly_wait(5000)
            self.more_btn()
        except NoSuchElementException:
            self.take_screenshot('无法找到更多按钮')
            self.fail('无法找到更多按钮')
        try:
            self.driver.implicitly_wait(1)
            self.sure_btn()
        except NoSuchElementException:
            pass
        try:
            self.driver.implicitly_wait(100)
            self.send_my_computer()
        except NoSuchElementException:
            self.take_screenshot('无法找到发送到电脑按钮')
            self.fail('无法找到发送到电脑按钮')
        sleep(2)
        self.driver.keyevent(4)
        sleep(1)
        self.driver.keyevent(4)
        sleep(2)

    def export_txt(self):
        """
        拍图识字导出为txt文件
        :return:
        """
        try:
            self.assertTrue(self.by_id(self.HW_ENV + 'ver_tv2'))
        except NoSuchElementException as e:
            self.take_screenshot('导出txt时未返回APP')
            return e
        else:
            self.txt_select()
        self.wechat_btn()
        self.driver.implicitly_wait(2000)
        self.wechat_computer()
        self.driver.implicitly_wait(2)
        self.wechat_share()
        self.driver.implicitly_wait(2)
        self.wechat_back()
        self.driver.implicitly_wait(2)
        self.complete_btn()